<template>
  <!-- 当点击 取消按钮 时，关闭弹窗 -->
  <div>
    <el-drawer
      :title="$t('printBoxCode.reprint')"
      :visible.sync="visible"
      size="65%"
      top="4vh"
      :append-to-body="true"
      @close="clearDialog()"
      v-loading="loding"
      :close-on-click-modal="false"
      :wrapperClosable="false"
    >
      <div class="pack">
        <ele-form v-model="dialogfrom" v-bind="formConfig" ref="form">
        </ele-form>
        <ele-page
          :otherParams="otherParams"
          :formConfig="searchConfig"
          ref="eleTable"
          :operateList="operateList"
          @handleOperateEvent="handleOperateEvent"
          :column="column"
          :getList="mesProdBarcodeReprint"
        >
        </ele-page>

        <div slot="footer" class="dialog-footer2">
          <el-button @click="clearDialog()">
            {{ $t("common.cancel") }}
          </el-button>
        </div>
      </div>
    </el-drawer>
    <el-dialog
      width="20%"
      :title="$i18n.t('mesUniqueCodePrinting.print')"
      :visible.sync="innerVisible"
      :append-to-body="true"
      @close="clearFormDialog()"
      v-loading="loding"
      :close-on-click-modal="false"
    >
      <el-form :model="form">
        <el-form-item
          :label="$t('mesProdOtherCode.printingTimes')"
          label-width="85px"
        >
          <el-input
            size="small"
            disabled
            v-model="form.printingTimes"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearFormDialog()">{{
          $t("common.cancel")
        }}</el-button>
        <el-button type="primary" @click="handleRequest">{{
          $t("common.submit")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from "@/api/common";
import { printTemplateInfoName } from "@/api/barcodeprint/mesBarcodeprintHistory";

import {
  mesProdBarcodeReprint,
  mesProdBarcodeReprintList,
  mesProdBarcodeReprintInfo,
  selectMesProdBarcodeReprintDetail,
  getPrintingTimes,
  rowDel,
} from "@/api/print/mesProdBarcodePrint";

import { setPrintData } from "@/utils/judge.js";
export default {
  name: "repairPrintTable",
  data() {
    let that = this;
    return {
      innerVisible: false,
      loding: false,
      printRow: {},
      form: { printingTimes: 0 },
      otherParams: { orderCode: "" },
      visible: false,
      dialogfrom: {},
      formConfig: {
        span: 24,
        isShowBackBtn: false,
        isShowSubmitBtn: false,
        labelPosition: "right",
        formDesc: {
          orderCode: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.workOrderCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          barcodePrintTemp: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.barcodePrintTemp2"),
            layout: 12,
            required: true,
            disabled: true,
          },
          prodCode: {
            type: "input",
            label: this.$i18n.t("mesUniqueCodePrinting.prodCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.prodName"),
            layout: 12,
            required: true,
            disabled: true,
          },
        },
      },
      mesProdBarcodeReprint,
      operateList: [
        {
          name: $i18n.t("common.add"),
          type: "primary",
          icon: "el-icon-circle-plus-outline",
          handleName: "add", //点击时返回的标识
        },
        {
          name: $i18n.t("common.refresh"),
          type: "primary",
          icon: "el-icon-refresh",
          handleName: "refresh", //点击时返回的标识
        },
      ],
      searchConfig: {
        inline: true,
        formDesc: {
          status: {
            layout: 7,
            type: "select",
            label: $i18n.t("common.status"),
            type: "select",
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              let res = await getDict("mes_barcode_reprint_status");
              return res.data.result || [];
            },
          },
          // isPrint: {
          //   layout: "6",
          //   type: "select",
          //   label: "是否打印",
          //   type: "select",
          //   prop: {
          //     value: "value",
          //     text: "description",
          //   },
          //   options: async (data) => {
          //     let res = await getDict("yes_or_not");
          //     return res.data.result || [];
          //   },
          // },
        },
      },
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          width: 60,
          fixed: "left",
        },
        // {
        //   label: $i18n.t("mesProdOrderPlan.codeStrategy"),
        //   prop: "orderCode",
        //   width: 120,
        // },
        {
          label: $i18n.t("mesUniqueCodePrinting.startBarcode"), //"补打起始唯一码",
          prop: "startBarcode",
          width: 210,
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.endBarcode"),
          prop: "endBarcode",
          width: 210,
        },

        {
          label: $i18n.t("mesUniqueCodePrinting.createByName"), //"申请人",
          prop: "createByName",
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.createTime"), // "申请时间",11
          prop: "createTime",
          width: 180,
        },
        {
          label: $i18n.t("common.status"), //"状态",
          prop: "status",
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            let res = await getDict("mes_barcode_reprint_status");
            return res.data.result || [];
          },
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.examineByName"), // "审核人",
          prop: "examineByName",
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.examineTime"), // "审核时间",
          prop: "examineTime",
          width: 180,
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.examineContent"), // "审核内容",
          prop: "examineContent",
        },
        {
          label: $i18n.t("mesUniqueCodePrinting.isPrint"), //"是否打印",
          prop: "isPrint",
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            let res = await getDict("yes_or_not");
            return res.data.result || [];
          },
        },
        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 160,
          fixed: "right",
          render: (h, { row }) => {
            let dom = <p></p>;
            if (row.status == "examinePass" && row.isPrint == 0) {
              dom = (
                <el-button
                  type="primary"
                  plain
                  size="mini"
                  onClick={() => {
                    that.print(row);
                  }}
                >
                  {$i18n.t("common.print")}
                </el-button>
              );
            }
            if (row.status == "noExamine") {
              dom = (
                <el-button
                  type="danger"
                  plain
                  size="mini"
                  onClick={() => {
                    that.rowDel(row);
                  }}
                >
                  {$i18n.t("common.delete")}
                </el-button>
              );
            }

            return dom;
          },
        },
      ],
      tableData: [],
      params: {},
    };
  },
  methods: {
    //初始化
    init(params) {
      this.params = params;
      this.otherParams = { orderCode: params.orderCode };
      this.visible = true;
      this.dialogfrom = params;
      console.log(11111111, params);
      this.refresh();
    },

    //删除
    rowDel(data) {
      this.$confirm(
        $i18n.t("mesUniqueCodePrinting.deleteSingle"),
        $i18n.t("common.tip"),
        {
          confirmButtonText: $i18n.t("common.ok"),
          cancelButtonText: $i18n.t("common.cancel"),
          type: "warning",
        }
      ).then(() => {
        this.loading = true;
        rowDel(data.id)
          .then((res) => {
            const message = res.data.message;
            this.$message.success(message || $i18n.t("common.deleteSuccess"));
            this.refresh();
          })
          .finally(() => {
            this.loading = false;
          });
      });
    },
    refresh() {
      this.$nextTick(() => {
        this.$refs.eleTable.refresh();
      });
    },
    clearFormDialog() {
      this.innerVisible = false;
      this.form.printingTimes = 0;
    },
    clearDialog() {
      this.visible = false;
      this.$emit("refreshList");
    },
    // 表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name;
      const fnc = {
        add: () => {
          // this.$parent.  = true;
          this.$parent.$refs.repairPrintModel.init({
            ...this.params,
            barcodePrintTemp: this.params.barcodePrintTemp,
            quantity: 0,
            type: 1,
          });
        },
        refresh: () => {
          this.refresh(false);
        },
      };
      fnc[data.handleName]();
    },
    //点击打印按钮
    async handleRequest(id) {
      let data = await mesProdBarcodeReprintInfo(this.printRow.id);
      let list = data.data.result;
      this.visible = false;
      // this.innerVisible = false;
      this.refresh(true);
      this.$parent.refresh(false);
      let { barcodePrintTempCode, barcodePrintTemp } = this.dialogfrom;
      setPrintData(
        {
          code: barcodePrintTempCode,
          name: barcodePrintTemp,
          status: 1,
        },
        list.detailList
      );
      this.$router.push("/splitPrint");
    },
    async print(row) {
      this.printRow = row;

      // // ---------获取打印次数-----------------
      // let res = await getPrintingTimes({ goodsCode: this.params.prodCode });
      // if (res.data.result && res.data.result.frequency) {
      //   this.form.printingTimes = res.data.result.frequency;
      // } else {
      //   this.form.printingTimes = 1;
      // }
      // this.form.printingTimes = 1;
      // this.innerVisible = true;

      this.handleRequest(row.id);
    },
  },
};
</script>
<style lang="scss" scoped>
.pack {
  padding: 20px;
  padding-top: 0px;
}
.dialog-footer {
  padding-top: 20px;
  display: flex;
  justify-content: end;
}
.dialog-footer2 {
  padding-top: 0px;
  display: flex;
  justify-content: flex-start;
  margin-top: -40px;
}
</style>
